package jupiter.servlets;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
 
import java.util.ArrayList;
import java.util.Collection;
import javax.servlet.http.HttpSession;
 
import org.json.*;
import org.mortbay.util.ajax.Continuation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jupiter.channel.*;
import jupiter.message.*;
 
public class SessionListener implements HttpSessionListener {
	Logger logger = LoggerFactory.getLogger(SessionListener.class);
	
	public void sessionCreated(HttpSessionEvent arg0) {
		logger.info("Creating session: "+arg0.getSession().getId());
	}
 
	public void sessionDestroyed(HttpSessionEvent arg0) {
		
		try{
			logger.info("Destroying session: "+arg0.getSession().getId());
			HttpSession  session = arg0.getSession();
			if(session == null){
				System.out.println("\n\nWTF2 no session either!");
			}
			Session sn = (Session) session.getAttribute("sessionChannel");
			if(sn == null){
				System.out.println("WTF!!!!!!!"+arg0.getSession().getId());
			} else {
				sn.destroy();
			}
		}catch(RuntimeException e){
			logger.error("RuntimeException in MessageServlet", e);
			throw e;
	    }catch(Exception e){
			logger.error("Exception in MessageServlet", e);
	    }
	}
	
 
}